package handler

import (
	"strconv"
	"video-hub/models"

	"github.com/gin-gonic/gin"
)

func VideoAdd(c *gin.Context) {
	title := c.PostForm("title")
	content := c.PostForm("content")
	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := models.Video{
		Title:   title,
		Content: content,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "发布失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "发布成功",
	})
	return
}
func VideoList(c *gin.Context) {
	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))
	offset := (page - 1) * size
	var video []models.Video
	models.DB.Offset(offset).Limit(size).Find(&video)
	var total int64
	models.DB.Model(models.Video{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  500,
		"msg":   "ok",
		"data":  video,
		"total": total,
	})
	return
}
